package com.neuedu.nep.controller;

import com.neuedu.nep.base.BaseController;
import com.neuedu.nep.base.R;
import com.neuedu.nep.dto.SignUpDTO;
import com.neuedu.nep.dto.SupervisorSignInDTO;
import com.neuedu.nep.model.Supervisor;
import com.neuedu.nep.service.SupervisorService;
import com.neuedu.nep.util.JwtUtil;
import com.neuedu.nep.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 黄宇佳
 * @create 2024-04-09 16:35
 * @description:
 */
@RestController
@CrossOrigin
@RequestMapping("supervisor")
public class SupervisorController extends BaseController {
    @Resource
    private SupervisorService supervisorService;

    @PostMapping("SignUp")
    public R SignUp(@RequestBody SignUpDTO signUpDTO){
        Supervisor supervisor=new Supervisor();
        BeanUtils.copyProperties(signUpDTO,supervisor);
        int result=supervisorService.SignUp(supervisor);
        if (result>0){
            return R.ok();
        }else if(result==0){
            return R.error();
        }else if (result==-1){
            return R.error("手机号已存在").setCode(501);
        }else{
            return R.error();
        }
    }

    @PostMapping("SignIn")
    public R SignIn(@RequestBody SupervisorSignInDTO supervisorSignInDTO){
        String TelId=supervisorSignInDTO.getTelId();
        String password=supervisorSignInDTO.getPassword();
        Supervisor supervisor = supervisorService.SignIn(TelId,password);
        if (supervisor==null){
            return R.error("手机号/密码错误");
        }
        supervisor.setPassword(null);
        UserVO userVO=new UserVO();
        userVO.setRole(0);
        userVO.setTelId(supervisor.getTelId());
        String token = JwtUtil.newToken(userVO);
        return R.ok("登陆成功")
                .setResult("supervisor",supervisor)
                .setResult("token",token);
    }
}
